package com.jinkaimen.kqb.ui.activity;

import android.os.Message;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.dao.AccountDao;
import com.jinkaimen.kqb.manager.ApiManager;

import retrofit2.Call;
import retrofit2.Response;

public class ChangePwdActivity extends BaseActivity {

    private EditText mTelView;
    private EditText mPwdView;
    private EditText mPwdNewView;
    private EditText mPwdNewNextView;
    private CheckBox mSeePwdView;
    private CheckBox mSeeNewPwdView;
    private CheckBox mSeeNewNextPwdView;
    private TextView mTvresetPwd;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("修改密码");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected void initChildView() {
        mTelView = (EditText) findViewById(R.id.et_activity_register_tel);
        mPwdView = (EditText) findViewById(R.id.et_activity_register_pwd);
        mPwdNewView = (EditText) findViewById(R.id.et_activity_register_new_pwd);
        mPwdNewNextView = (EditText) findViewById(R.id.et_activity_register_new_next_pwd);
        mSeePwdView = (CheckBox) findViewById(R.id.cb_activity_register_see_pwd);
        mSeeNewPwdView = (CheckBox) findViewById(R.id.cb_activity_register_see_new_pwd);
        mSeeNewNextPwdView = (CheckBox) findViewById(R.id.cb_activity_register_see_new_next_pwd);
        mTvresetPwd = (TextView) findViewById(R.id.tv_activity_login_forget_pwd);
    }

    @Override
    protected void initChildData() {
        String name = SharedPreferencesUtils.getString("name", "");
        mTelView.setText(name);
    }

    @Override
    protected void setChildViewListener() {
        mSeePwdView.setOnClickListener(this);
        mSeeNewPwdView.setOnClickListener(this);
        mSeeNewNextPwdView.setOnClickListener(this);
        mTvresetPwd.setOnClickListener(this);
        findViewById(R.id.btn_activity_register).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()) {

            case R.id.btn_activity_register:
                String pwd = mPwdView.getText().toString();
                String pwd1 = mPwdNewView.getText().toString();
                String pwd2 = mPwdNewNextView.getText().toString();
                if (pwd1.length() < 6 || pwd1.length() > 12) {
                    UIUtils.showToast("密码为6-12位,请重新输入");
                    return;
                }
                if (!pwd1.equals(pwd2)) {
                    UIUtils.showToast("两次密码输入不一致");
                    return;
                }
                if (pwd2.length() < 6 || pwd2.length() > 12) {
                    UIUtils.showToast("密码为6-12位,请重新输入");
                    return;
                }
                long id = AccountDao.getInstance().getAccount().id;
                showLoadingDialog();
                ApiManager.getApi().changePwd(
                        "HSY001063",
                        id,
                        pwd,
                        pwd1
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        UIUtils.showToast("重置密码成功");
                        ActivityUtils.finishActivity(ChangePwdActivity.this);
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }

                });
                break;
            case R.id.cb_activity_register_see_pwd:
                if (mSeePwdView.isChecked()) {
                    mPwdView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPwdView.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.cb_activity_register_see_new_pwd:
                if (mSeeNewPwdView.isChecked()) {
                    mPwdNewView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPwdNewView.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.cb_activity_register_see_new_next_pwd:
                if (mSeeNewNextPwdView.isChecked()) {
                    mPwdNewNextView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPwdNewNextView.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.tv_activity_login_forget_pwd:
                ActivityUtils.startActivity(ChangePwdActivity.this, ResetPwdActivity.class);
                break;
        }
    }

    @Override
    protected void handleMessage(BaseActivity context, Message msg) {
        switch (msg.what) {
            case 100:
                break;
        }
    }
}
